<template>
  <div class="materialInputs">
    <div style="padding-left: 20px;">
      <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
              <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['materialInputs:materialInputs:add']">新增</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button type="success" icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate" v-hasPermi="['materialInputs:materialInputs:edit']">修改</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['materialInputs:materialInputs:remove']">删除</el-button>
            </el-col>
      </el-row>
      <el-table v-loading="loading" :data="materialInputsList" @selection-change="handleSelectionChange" :height="tableHeight" custom-class="show-back-class">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="材料类型" align="center" prop="materialType" >
              <template slot-scope="scope">
                {{selectDictLabel($store.state.referenceDict.materialTionType, scope.row.materialType)}}
              </template>
            </el-table-column>
            <el-table-column label="材料类型和计量单位" align="center" prop="materialAndUnit">
              <template slot-scope="scope" v-if="scope.row.materialType == 1">
              {{selectDictLabel(referenceMaterialList, scope.row.materialAndUnit,'id','material')}}
              </template>
            </el-table-column>
            <el-table-column label="年消耗量" align="center" prop="quantityPerYear" />
            <el-table-column label="材料引起排放的百分比" align="center" prop="materialPercentage" />
            <el-table-column label="财务分类" align="center" prop="financialCategory">
              <template slot-scope="scope" v-if="scope.row.materialType == 2">
              {{selectDictLabel(referenceMaterialList, scope.row.financialCategory,'id','economicSector')}}
              </template>
            </el-table-column>
            <el-table-column label="年价值" align="center" prop="valuePerYear" />
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
              <template slot-scope="scope">
                <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['materialInputs:materialInputs:edit']">修改</el-button>
                <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['materialInputs:materialInputs:remove']">删除</el-button>
              </template>
            </el-table-column>
      </el-table>
      <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList"/>
    </div>

    <!-- 添加或修改原材料投入对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body custom-class="show-back-class">
      <el-form ref="form" :model="form" :rules="rules" label-width="160px">
        <el-form-item label="材料类型" prop="materialType">
          <el-select v-model="form.materialType" placeholder="请选择材料类型">
            <el-option class="index-option" v-for="dict in  $store.state.referenceDict.materialTionType" :key="dict.dictValue"
                       :label="dict.dictLabel" :value="parseInt(dict.dictValue)" />
          </el-select>
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input v-model="form.description" placeholder="描述"/>
        </el-form-item>
        <el-form-item label="材料引起排放的百分比" prop="materialPercentage">
          <el-input v-model="form.materialPercentage" placeholder="请输入材料引起排放的百分比"/>
        </el-form-item>
        <el-row v-if="form.materialType===1">
          <el-form-item label="材料类型和计量单位" prop="materialAndUnit" >
            <el-select v-model="form.materialAndUnit" placeholder="请选择材料类型和计量单位" style="width: 100%"  >
              <el-option class="index-option" v-for="dict in  referenceMaterialList" :key="dict.id" v-if="dict.materialType===1"
                         :label="dict.material + ' - ' + dict.unit" :value="parseInt(dict.id)" />
            </el-select>
          </el-form-item>
          <el-form-item label="年消耗量" prop="quantityPerYear">
            <el-input v-model="form.quantityPerYear" placeholder="请输入年消耗量"/>
          </el-form-item>
        </el-row>

        <el-row v-if="form.materialType===2">
          <el-form-item label="财务分类" prop="financialCategory" >
            <el-select v-model="form.financialCategory" placeholder="请选择财务分类" style="width: 100%"  >
              <el-option class="index-option" v-for="dict in  referenceMaterialList" :key="dict.id" v-if="dict.materialType===2"
                         :label="dict.economicSector" :value="parseInt(dict.id)" />
            </el-select>
          </el-form-item>
          <el-form-item label="年价值" prop="valuePerYear">
            <el-input v-model="form.valuePerYear" placeholder="请输入年价值"/>
          </el-form-item>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMaterialInputs, getMaterialInputs, delMaterialInputs, addMaterialInputs, updateMaterialInputs, exportMaterialInputs } from "@/api/gasEmission/materialInputs";
import { materialDict,listMaterial} from "@/api/reference/material";
export default {
  name: "MaterialInputs",
  props:{
    enterpriseId:{
      type: Number,
      required:true,
    }
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 原材料投入表格数据
      materialInputsList: [],
      referenceMaterialList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        materialType: null,
        description: null,
        materialAndUnit: null,
        quantityPerYear: null,
        materialPercentage: null,
        financialCategory: null,
        valuePerYear: null,
        referenceId: null,
        infoId: this.enterpriseId
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      tableHeight: ''
    };
  },
  created() {
    this.tableHeight = 600;
    listMaterial().then(res=>{
      if(res.rows){
        this.referenceMaterialList=res.rows
      }
    });
    this.getList();
  },
  methods: {
    /** 查询原材料投入列表 */
    getList() {
      this.loading = true;
      listMaterialInputs(this.queryParams).then(response => {
        this.materialInputsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        materialType: null,
        materialAndUnit: null,
        quantityPerYear: null,
        materialPercentage: null,
        financialCategory: null,
        valuePerYear: null,
        referenceId: null,
        infoId: this.enterpriseId
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加原材料投入";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getMaterialInputs(id).then(response => {
        this.form = response.data;
        this.form.materialAndUnit = parseInt(this.form.materialAndUnit);
        this.form.financialCategory = parseInt(this.form.financialCategory);
        this.open = true;
        this.title = "修改原材料投入";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateMaterialInputs(this.form).then(response => {
              if (response.code === '200') {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addMaterialInputs(this.form).then(response => {
              if (response.code === '200') {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除原材料投入编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delMaterialInputs(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有原材料投入数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportMaterialInputs(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>
<style scoped lang="scss">
  .materialInputs {
    width: 100%;
    height: 100%;
  }
  .dialog-footer {
    text-align: right;
  }
  
  .el-table {
    height: 100%;
    width: 100%;
  }
</style>